package com.tron.dobest.word;

import com.deepoove.poi.data.MiniTableRenderData;
import com.deepoove.poi.data.style.TableStyle;
import com.deepoove.poi.util.TableTools;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description word文档处理
 * @auther TRON
 * @create 2020-02-12
 */
public class Start {
    public static void main(String[] args) {
        //创建一个word
//        createWord();
        updateWord();
    }


    /**
     * 创建一个word
     */
    private static void createWord() {
        XWPFDocument doc = new XWPFDocument();
        //创建一个段落
        XWPFParagraph paragraph = doc.createParagraph();
        //追加段落文本
        XWPFRun run0 = paragraph.insertNewRun(0);
        XWPFRun run1 = paragraph.insertNewRun(1);
        run0.setText("第0个文本添加一点内容");
        run0.addCarriageReturn();   //文本换行
        run1.setText("第1个文本添加一点内容");

        //创建表格
        XWPFTable table = doc.createTable(3, 3);
        table.getRow(0).getCell(0).setText("EXAMPLE OF TABLE");
        table.getRow(0).getCell(1).setText("EXAMPLE OF TABLE");
        table.getRow(0).getCell(2).setText("{time}");


        //合并第一行的第0列到第8列单元格
//        TableTools.mergeCellsHorizonal(table, 1, 0, 8);
        // 合并第0列的第一行到第九行的单元格
//        TableTools.mergeCellsVertically(table, 0, 1, 9);

        //设置表格宽度
        TableTools.widthTable(table, MiniTableRenderData.WIDTH_A4_FULL,3);
        // 设置表格居中
        TableStyle style = new TableStyle();
        style.setAlign(STJc.CENTER);
        TableTools.styleTable(table, style);

        try (FileOutputStream out = new FileOutputStream("src/main/java/com/tron/dobest/word/simple.docx")) {
            doc.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void updateWord() {
        XWPFDocument doc = null;
        try {
            doc = new XWPFDocument(new FileInputStream("src/main/java/com/tron/dobest/word/simple.docx"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 段落
        List<XWPFParagraph> paragraphs = doc.getParagraphs();
        for (XWPFParagraph xwpfParagraph : paragraphs) {
            List<XWPFRun> runs = xwpfParagraph.getRuns();
            for (XWPFRun xwpfRun : runs) {
                System.out.println(xwpfRun.getText(0));
            }
        }
        // 表格
        List<XWPFTable> tables = doc.getTables();
        // 图片
        List<XWPFPictureData> allPictures = doc.getAllPictures();
        // 页眉
        List<XWPFHeader> headerList = doc.getHeaderList();
        // 页脚
        List<XWPFFooter> footerList = doc.getFooterList();

        //表数量
//        System.out.println(tables.size());

        //读取第一张表
        XWPFTable xwpfTable = tables.get(0);

        //读取表中所有行
        List<XWPFTableRow> rows = xwpfTable.getRows();

        //读取每一行的内容
        for (XWPFTableRow xwpfTableRow : rows) {
            List<XWPFTableCell> tableCells = xwpfTableRow.getTableCells();
            for (XWPFTableCell xwpfTableCell : tableCells) {
                //读取列内容
                String text = xwpfTableCell.getText();
                //判断是否属于占位符，如果属于占位符，替换指定内容。
                String s = matchString(text);
                if (false) {
                    //移除原来的内容
                    xwpfTableCell.removeParagraph(0);
                    //添加新的内容
                    XWPFParagraph xwpfParagraph = xwpfTableCell.addParagraph();
                    XWPFRun run = xwpfParagraph.createRun();
                    run.setText("*添加内容*");
                }
            }
        }
        try (FileOutputStream out = new FileOutputStream("src/main/java/com/tron/dobest/word/out.docx")) {
            doc.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //获取站位符内容
    public static String matchString(String str) {
        Matcher matcher = Pattern.compile("(?<=" + "<#" + ").*?(?=" + "#>" + ")").matcher(str);
        String s = "";
        while (matcher.find()) {
            s = matcher.group();
        }
        return s;
    }
}
